﻿Ext.require(['AM.service.Support',
    'AM.util.URL'
]);

Ext.define('AM.service.Insert', {

    singleton:true,

    insertShapeByName: function(name) {
        var cmp =  Ext.create('AM.component.shape.' + name)
        AM.service.Insert._insert(cmp);
        AM.service.Support.drag(cmp);
        AM.service.Support.contextmenu(cmp);
    },
    insertImg:function () {
        var cmp = arguments[0];
        var file = cmp.fileInputEl.dom.files[0]
        var img = Ext.create("AM.component.Image", {file:file});
        AM.service.Insert._insert(img);
    },

    insertImgByName: function(name) {
         var img = Ext.create('AM.component.Image', null, {'image-name': name })
        AM.service.Insert._insert(img);
    },




    insertRemoteAudio: function(){
        Ext.Ajax.request({
            url:'GetAudios',

            success:function (response) {
                var text = response.responseText;
                var names = text.split('|');
                var items = []

                Ext.each(names, function (name) {
                    if (-1 != name.indexOf('.mp3') || -1 != name.indexOf('.wav'))
                        items.push({
                            xtype:'button',
                            text: name,
                            handler: function() {
                               var audio = Ext.create("AM.component.Audio", null, {'audio-name': name});
                                AM.service.Insert._insert(audio);
                            }
                        })
                })
                Ext.create('Ext.window.Window',
                    {
                        title: '选择音乐',
                        autoScroll:true,
                        items:items, width:500, height:300
                    }).show();

            }

        })
    },

    insertText:function () {
        var txt = Ext.create('AM.component.Text');
        AM.service.Insert._insert(txt);
    },


    insertAudio:function () {
        var cmp = arguments[0];
        var file = cmp.fileInputEl.dom.files[0];
        var audio = Ext.create('AM.component.Audio', {file:file});
        AM.service.Insert._insert(audio);
    },


    insertVideo:function () {
        var cmp = arguments[0];
        var file = cmp.fileInputEl.dom.files[0];
        var reader = new FileReader();
        reader.onload = function (re) {
            var audio = Ext.create('AM.component.Video', {src:re.target.result});
            AM.service.Insert._insert(audio);
        }
        reader.readAsDataURL(file);
    },


    insertTable:function () {
        Ext.Msg.prompt('添加表格', '请填写表格的行列数，空格区分 例如：5 6', function (btn, text) {
            if (btn == 'ok') {
                var arr = text.split(' ');
                var rowNum = arr[0];
                var colNum = arr[1];

                var table = Ext.create('AM.component.Table', {
                    rowNum:rowNum,
                    colNum:colNum
                });
                AM.service.Insert._insert(table);
            }
        })
    },


    insertPageLink:function () {
        var text = prompt('输入页码');
        var index = parseInt(text);
        var url = '#link(' + index + ')';
        document.execCommand('createlink', false, url);
        var page = ppt.view.workspace.items.get(ppt.index);
        var as = page.el.select('a[href^=#link(]').elements
        var len = as.length;
        for (var i = 0; i < len; i++) {
            var a = as[i];
            var href = a.href;
            var pageIndex = href.substring(href.indexOf('(') + 1, href.indexOf(')'));
            pageIndex = parseInt(pageIndex);
            a = Ext.get(a);
            a.addListener('click', function () {
                AM.service.WorkSpace.go(pageIndex);
            });
        }
    },


    _insert:function (object) {
        var currentPage = ppt.view.workspace.items.get(ppt.index);
        currentPage.add(object);

        AM.service.Thumbnails.refresh(ppt.index);
    }
})